package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Exam;
import cn.wolfcode.domain.ExamQuestion;
import cn.wolfcode.enums.QuestionTypeEnum;
import cn.wolfcode.exception.BusinessException;
import cn.wolfcode.mapper.ExamMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IExamService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExamServiceImpl implements IExamService {

    @Autowired
    private ExamMapper examMapper;

    @Override
    public void save(Exam exam) {
        examMapper.insert(exam);
    }

    @Override
    public void delete(Long id) {
        examMapper.deleteRelation(id);
        examMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Exam exam) {
        examMapper.updateByPrimaryKey(exam);
    }

    @Override
    public Exam get(Long id) {
        return examMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Exam> listAll() {
        return examMapper.selectAll();
    }

    @Override
    public PageInfo<Exam> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize()); //对下一句sql进行自动分页
        List<Exam> exams = examMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Exam>(exams);
    }

    /**
     * 创建试卷
     * @param exam 试卷对象
     */
    @Override
    public void createExam(Exam exam) {
        //获取题库每种题型的题目数量
        int radioCount = examMapper.queryQuestionCount(QuestionTypeEnum.RADIO.getValue());
        int checkboxCount = examMapper.queryQuestionCount(QuestionTypeEnum.CHECKBOX.getValue());
        int judgeCount = examMapper.queryQuestionCount(QuestionTypeEnum.JUDGE.getValue());
        List<Long> radioList = null;
        List<Long> checkboxList = null;
        List<Long> judgeList = null;
        //判断题库数量是否符合创建试卷的题目数量
        if (exam.getRadioNum() != null && exam.getRadioNum() > radioCount) {
            throw new BusinessException("单选题目题库不足,请重新输入");
        } else if (exam.getRadioNum() != null && exam.getRadioNum() > 0){
            radioList = examMapper.queryByQuestion(QuestionTypeEnum.RADIO.getValue(), exam.getRadioNum());
        }
        if (exam.getCheckboxNum() != null && exam.getCheckboxNum() > checkboxCount) {
            throw new BusinessException("多选题目题库不足,请重新输入");
        } else if (exam.getCheckboxNum() != null && exam.getCheckboxNum() > 0){
            checkboxList = examMapper.queryByQuestion(QuestionTypeEnum.CHECKBOX.getValue(), exam.getCheckboxNum());
        }
        if (exam.getJudgeNum() != null && exam.getJudgeNum() > judgeCount) {
            throw new BusinessException("判断题目题库不足,请重新输入");
        } else if (exam.getJudgeNum() != null && exam.getJudgeNum() > 0){
            judgeList = examMapper.queryByQuestion(QuestionTypeEnum.JUDGE.getValue(), exam.getJudgeNum());
        }
        //插入试卷以及试卷问题的中间表关系
        examMapper.insert(exam);
        insertRelation(radioList, exam.getId(), exam.getRadioScore());
        insertRelation(checkboxList, exam.getId(), exam.getCheckboxScore());
        insertRelation(judgeList, exam.getId(), exam.getJudgeScore());
    }


    /**
     * 创建试卷问题中间表关系
     * @param list 题目集合
     * @param examId 试卷ID
     * @param score 对应题型每题分数
     */
    private void insertRelation(List<Long> list, Long examId, Integer score){
        int count = 0;
        if (list != null && list.size() > 0) {
            for (Long aLong : list) {
                ExamQuestion examQuestion = new ExamQuestion();
                examQuestion.setExamId(examId);
                examQuestion.setQuestionId(aLong);
                examQuestion.setSequence(++count);
                examQuestion.setScore(score);
                examMapper.saveRelation(examQuestion);
            }
        }
    }
}
